from tkinter.messagebox import showwarning

import requests
import re


url = 'http://192.168.0.7:8080//services/GZJDDeviceService'

def GetBluetoothAddressBySN(barCode:str, url=url):
    headers = {
        'Content-Type':'text/xml'
    }
    flag = False
    # barCode = 'VG0015-BO0200349'
    # barCode = '5PDPYYY01ZA501845180108'
    body = f'''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                     xmlns:ser="http://service.jgmes.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:GetBluetoothAddressBySN>
             <barCode>{barCode}</barCode>  
          </ser:GetBluetoothAddressBySN>
       </soapenv:Body>
    </soapenv:Envelope>
    '''
    BTMAC = 'NO'
    try:
        res = requests.post(url,headers=headers,data=body)
        print(res.status_code)
        if res.status_code == 200:
            try:
                match_res_str = re.findall('<data .*>(.*?)</data>', res.text)
                BTMAC = match_res_str[0]
                flag = True
                return flag, BTMAC
            except Exception as e:
                showwarning(message='访问服务器成功，数据处理失败')
                return flag, BTMAC
        else:
            showwarning(message='访问服务器失败')
            return flag, BTMAC
    except Exception as e:
        showwarning(message=f'请求访问服务器发生问题, {e}')
        return flag, BTMAC

if __name__ == '__main__':
    barCode = '5PDPYYY01ZA501845180108'
    flag, BTMAC = GetBluetoothAddressBySN(barCode=barCode, url=url)
    print(flag, BTMAC)